package com.pengtu.gsj.service.impl.yh;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.yh.ReconstructionDao;
import com.pengtu.gsj.entity.base.LabelValue;
import com.pengtu.gsj.entity.yh.Reconstruction;
import com.pengtu.gsj.service.yh.ReconstructionService;
import com.pengtu.gsj.utils.LabelHelper;

@Transactional
@Service
public class ReconstructionServiceImpl implements ReconstructionService{

	@Autowired
	private ReconstructionDao reconstructionDao;
	
	@Override
	public Reconstruction getReconstruction(String id) {
		return reconstructionDao.get(id);
	}

	@Override
	public void delete(Reconstruction reconstruction) {
		reconstructionDao.delete(reconstruction);
	}

	@Override
	public Page<Reconstruction> findReconstructionList(Page<Reconstruction> page, QueryFilter queryFilter) {
		return reconstructionDao.findPage(page, queryFilter);
	}

	@Override
	public void save(Reconstruction reconstruction) {
		reconstructionDao.save(reconstruction);
	}

	@Override
	public List<List<Map<String, Object>>> count() {
		List<List<Map<String, Object>>> list = new ArrayList<List<Map<String,Object>>>();
		List<Map<String, Object>> innerList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> yearList = null;
		Map<String, Object> map = null;
		QueryFilter queryFilter = null;
		List<Reconstruction> reconstructions = reconstructionDao.getCountReconstructions();
		for (Reconstruction reconstruction : reconstructions) {
			List<Reconstruction> recons = reconstructionDao.findBy("projectType", reconstruction.getProjectType());
			map = new HashMap<String, Object>();
			map.put("name", LabelHelper.getInstance().buildCodeName(5001, reconstruction.getProjectType()));
			map.put("all", recons.size());
			map.put("finished", reconstruction.getCount());
			map.put("rate", new BigDecimal(Double.parseDouble(String.valueOf(reconstruction.getCount()))*100/recons.size()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
			innerList.add(map);
		}
		list.add(innerList);
		List<LabelValue> labels = LabelHelper.getInstance().buildCodeNames(5001);
		for (LabelValue labelValue : labels) {
			yearList = new ArrayList<Map<String,Object>>();
			List<Reconstruction> yeaReconstructions = reconstructionDao.getcountReconsGroupByType(labelValue.getValue());
			Long projectNumber = 0L;
			Long finishedProjectNumber = 0L;
			Double totalSubsidy = 0.0;
			Double totalSelf = 0.0;
			for (Reconstruction reconstruction : yeaReconstructions) {
				map = new HashMap<String, Object>();
				queryFilter = new QueryFilter();
				queryFilter.addFilter("projectYear", reconstruction.getProjectYear(),MatchType.EQ);
				queryFilter.addFilter("projectType", reconstruction.getProjectType(),MatchType.EQ);
				queryFilter.addFilter("finished", "1",MatchType.EQ);
				List<Reconstruction> reconstructs = reconstructionDao.find(queryFilter);
				map.put("year", reconstruction.getProjectYear());
				map.put("all", reconstruction.getCount());
				map.put("finished", reconstructs.size());
				map.put("spare", reconstruction.getCount()-reconstructs.size());
				map.put("total", reconstruction.getTotalInvestment());
				map.put("subsidy", reconstruction.getSubsidyAmount());
				map.put("selfRaised", reconstruction.getSelfRaised());
				map.put("rate",new BigDecimal(Double.parseDouble(reconstruction.getRate())).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
				map.put("finishRate",new BigDecimal(Double.parseDouble(String.valueOf(reconstructs.size()))*100/reconstruction.getCount()).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
			    finishedProjectNumber += reconstructs.size();
			    projectNumber += reconstruction.getCount();
				totalSubsidy += reconstruction.getSubsidyAmount();
				totalSelf += reconstruction.getSelfRaised();
				yearList.add(map);
			}
			map = new HashMap<String, Object>();
			map.put("year","总计");
			map.put("all", projectNumber);
			map.put("finished", finishedProjectNumber);
			map.put("spare", projectNumber-finishedProjectNumber);
			map.put("total", totalSelf+totalSubsidy);
			map.put("subsidy", totalSubsidy);
			map.put("selfRaised", totalSelf);
			map.put("rate",new BigDecimal(totalSelf*100/(totalSelf+totalSubsidy)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
			map.put("finishRate",new BigDecimal(Double.parseDouble(String.valueOf(finishedProjectNumber))*100/projectNumber).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
			
			yearList.add(map);
			list.add(yearList);
		}
		return list;
	}
	
	@Override
	public List<Reconstruction> getReconstruction(QueryFilter queryFilter){
		return reconstructionDao.find(queryFilter);
	}
}
